package com.gridmobi.xrtb.handler.monitor;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component
public class IpMonitor {
    private static final Logger logger = LoggerFactory.getLogger(IpMonitor.class);


    @Value("${rtbimpiplimit}")
    private int iplimit = 10;
    /**
     * 个个节点通过kafka共享数据时候 需要一个独立的消费组 不然节点之间数据不共享
     */
    public static final String IP_CONSUMER_GROUP = UUID.randomUUID().toString();

    public static  Cache<String, Object> ipcache = CacheBuilder.newBuilder()
            /*设置缓存容器的初始容量大小为10*/
            .initialCapacity(100000)
            /*设置缓存容器的最大容量大小为100*/
            .maximumSize(1000000)
            /*设置记录缓存命中率*/
            .recordStats()
            /*设置并发级别为8，智并发基本值可以同事些缓存的线程数*/
            .concurrencyLevel(32)
            /*设置过期时间为2秒*/
            .expireAfterWrite(30, TimeUnit.MINUTES).build();

    public Cache<String, Object> getIpcache() {
        return ipcache;
    }

    public void setIpcache(Cache<String, Object> ipcache) {
        this.ipcache = ipcache;
    }



    public boolean existisBlockIp(String ip) {
        try {
            if (ipcache.getIfPresent(ip) !=null) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void main(String[] args) {

        ipcache.put("aaa",1111);
        System.out.println(ipcache.getIfPresent("aaa"));
    }
}
